package com.cash.lemoloan.statisticlib.Utils;

import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;


import com.cash.lemoloan.statisticlib.StatisticApplication;
import com.cash.lemoloan.statisticlib.StatisticContant;
import com.cash.lemoloan.statisticlib.model.StatisticAPP01Info;

import java.util.ArrayList;
import java.util.List;


public class AppInfoUtil {
    public static synchronized List<StatisticAPP01Info> getAppInfo(){
        List<StatisticAPP01Info> app01InfoList = new ArrayList<StatisticAPP01Info>();

        PackageManager packageManager = StatisticApplication.getInstance().getPackageManager();
        List<PackageInfo> packages = packageManager.getInstalledPackages(0);
        for (int i = 0; i < packages.size(); i++) {
            StatisticAPP01Info app01Info = new StatisticAPP01Info();
            PackageInfo packageInfo = packages.get(i);
            if ((packageInfo.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) != 0) {
                app01Info.setstatisticAppType(StatisticContant.SYSTEM);
            }else{
                app01Info.setstatisticAppType(StatisticContant.NON_SYSTEM);
            }
            app01Info.setstatisticAppName(packageInfo.applicationInfo.loadLabel(packageManager).toString());
            app01Info.setstatisticPackageName(packageInfo.packageName);
            app01Info.setstatisticCtime(String.valueOf(System.currentTimeMillis()));
            String firstInstallTime = String.valueOf(packageInfo.firstInstallTime);
            if(firstInstallTime.length() == 10){
                firstInstallTime += "000";
            }
            app01Info.setstatisticFirstInstallTime(firstInstallTime);
            String lastUpdateTime = String.valueOf(packageInfo.lastUpdateTime);
            if(lastUpdateTime.length() == 10){
                lastUpdateTime += "000";
            }
            app01Info.setstatisticLastUpdateTime(lastUpdateTime);
            app01Info.setstatisticVersionCode(String.valueOf(packageInfo.versionCode));
            app01Info.setstatisticVersionName(String.valueOf(packageInfo.versionName));
            app01InfoList.add(app01Info);

        }
        return app01InfoList;
    }
}
